{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Gotchas\n",
    "\n",
    "### Symbols\n",
    "\n",
    "`sympy` 可以用在任何的 Python 环境中. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sympy import *\n",
    "init_printing(use_unicode=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "在 `sympy` 中, 变量是不会自动定义的, 需要使用 `symbols` 函数定义变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/latex": [
       "$\\displaystyle x + 1$"
      ],
      "text/plain": [
       "x + 1"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = symbols('x')\n",
    "x + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`symbols` 还可以接受一个字符串来定义多个变量, 字符串中的变量需要用**空格**分隔. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "x, y, z = symbols('x y z')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> `symbols` 中接受的变量不一定要和 Python 中定义的变量名一致, 如: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b\n",
      "a\n"
     ]
    }
   ],
   "source": [
    "a, b = symbols('b a')\n",
    "print(a)\n",
    "print(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> 但是最好我们定义成一致的, 这样方便后面的计算, 也可以提高代码的可读性"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "SymPy Symbol 和 Python variables 是不一样的, 如: "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "x + 1\n"
     ]
    }
   ],
   "source": [
    "x = symbols('x')\n",
    "expr = x + 1\n",
    "x = 2 \n",
    "print(expr)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "上面的例子说明: 如果一个变量改变了, 但是通过这个变量创建的表达式并不会自动改变. "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果想在表达式中带入 `x` 的值, 可以使用 `.subs` 属性来替换相应的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAOCAYAAAAWo42rAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA2klEQVQoFXWS0Q2CQBBEgViAtGAJRjugBGkBSjD+8UsJWoJSAiWorUgHOG+5vZwkbrLuzNzsLXsxn+c5I7qu26m0RrJsqwrvpY9oOUYRDhDdiNZIuypr4aEQIBAbCSdjy8894AvVjW/hKSQ6N8JjbEAS+Y4yqovmtzM+3ph6aKwk9MpW+Mah3QggJO5VMB2VfM5TaWFbO0mrmhj9UNrWf400yfxR4elK25qRJIer8NFVIQMdLzLglXehGP39hoBT4yGQ0bc+p6dgNbEM03iiKS4jwrPUSo+fP8UX4QdVmcOnmX8AAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle 3$"
      ],
      "text/plain": [
       "3"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = symbols('x')\n",
    "expr = x + 1\n",
    "expr.subs(x, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Equals signs\n",
    "\n",
    "由于在 Python 中等号是用来赋值的, 所以在 `sympy` 中表示等式需要另外的函数, `Eq()`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAFUAAAAQCAYAAABnYDOFAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACFElEQVRYCe2Y3U1CQRBGgVAAsQRLUOlAO9BQgdKBxDdesQO1BO0AOkAogRKQDvCcm90NyAUxevUCTjLM/rKz334zu1Cdz+eVsku32z3Bx2f0lPKsbP4G/9rYtr7Vy+Zg9AcHG5Sf0Cl6hh6jZRUPfBydKxRUgDlnoWPsY1xwW8scGXnleMq3GNlaOgm+LflVW6r9fEW2qXspAOpBe/hqkqJBTQvtaaEFsCtRmMKfThl1EzbfxF6j5rFWaBsy5iWUD96AhSnpIQ+IBCqdPQZmt1eY4CUhkB3UyT10Z0ANPpvTvyJj5mV5fNMkxki2GXaSNy4DlU4ZKmiLckmlExqOsB/7F8eWrsyeMoIU5JjPp4jNyhKRqa8fUL9gpKeWnQR24+nRL5PzWOFhVOjP2+BWrHB+mYS9SMDcsI9+RqamN1boEKC1JxEnR7sGNMGU7T6p7uPYXbbsw7BvYHPDPu4tMjXWK0yIjEv5k7bsWYRdejqkSSUs4Ou66Nnk7WfRI6hNvtvH/qL4tJI8tk/qFATMihfVABsvq8XTuKNva+byHX8u+JuXcr7lV8BHjJaE9jcaBtgsTdaoyEx1SqMA+7MwMZI2+4bov6xHQNzUTOp8irwPWMGrAKI3Wx81fEaoYKdU4JjfEtY1gnTW3/7KiDbzv0+9P8/T+CBG0bdz6n3q/WqR/1KxyF5dVAC2lRj+RYppRD0oeQcI6bYl/YHBhwAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle x + 1 = 4$"
      ],
      "text/plain": [
       "x + 1 = 4"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Eq(x + 1, 4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "如果要比较两个等式表达式, a 和 b, 是否相等, 在 sympy 中不能用 `==` 来比较. 因为 `==` 是精确的比较两边的内容, 也就是说两边的内容不仅值要一样, 还必须形式相同. 如果要比较两个表达是的值是否相等, 我们可以验证 a - b 是否等于零. 如果等于零, 那么 a 和 b 相等. 对于表达的比较需要用到化简函数, `simplify()`, 来得出 a - b 的结果. 举例如下:  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAAoAAAAOCAYAAAAWo42rAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAA0UlEQVQoFXWR0Q2CQBBEhVgAwQ60BLUCsQO0A+3BP/4MJWgLWgIdGCmBDiB2gG+OW0PIuckyO3Ozu5cj6vt+ZlEURenrFlyRJVojLTIjwht+BZ86ABNA2p66ib14BhMzee0Dqukm7ozggawlTOIFzzTdjBlCNzGJaqoii/1dBvr/m2pi6s+te2y3Lb/V48NQvdBE6woZbFurO9rKJOA0bXhHDBW5DBhtYmXP88C0CRjXaLW2OiPFHaEDczNTa+2RPEmb6+ND3RcMdoUtfAd3f+wLrOY/XeyA6s8AAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle 0$"
      ],
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = (x + 1)**2\n",
    "b = x**2 + 2*x + 1\n",
    "simplify(a - b)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAABUAAAAOCAYAAADABlfOAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABTElEQVQ4EY2T4U3DMBCFTcUAESN0hIpuEDYAdQJgA/iZ/DUbFCZAYYN2g0I3oCOUbBC+l1yCldoNJ73Y99757IvPrmkaF0NRFAuwjmlT3MylrUK6SstpJZq0LMun9JJp5SQpCRcsqw3TGSIRJ0mJWZH4NRL7b+oyjLSy1yE3nhOTwT0Yv2S8B3OwMm43nJRgCTXjwcTU4Il5EQjYgTeQ4z8zakM/JMV5RDhbNrpO6EFotzgfRqhbfFu+BZ8t2xZ9EhtWcgO/7znGO8XNmKjsrBdEpoyY/UjL8d9HnNNJlXTJAjV7aGqtufEHRv2zwfCVUNaX7uD0T130ieoZ8kR/QNU/SeYZ2IDc9Io5079nju/lhxfVbhJ8tGu3c0fqZMLRTnRkXneS0ymlqRvchTKHhqgLuwYqX7YFG6DO0M1/AyVRW+nmdVlfQJu1v+IXXNfOUn0g3a0AAAAASUVORK5CYII=\n",
      "text/latex": [
       "$\\displaystyle 4 x$"
      ],
      "text/plain": [
       "4⋅x"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = x**2 - 2*x + 1\n",
    "simplify(a - c)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "同时, sympy 中也提供了一个函数, `equals()` 用于比较两个表达是否相等. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = cos(x)**2 - sin(x)**2\n",
    "b = cos(2*x)\n",
    "a.equals(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Two Final Notes: `^` and `/`\n",
    "\n",
    "在 Python 中, `^` 表示异或运算. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True ^ True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "True ^ False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAACoAAAAXCAYAAAB9J90oAAAABHNCSVQICAgIfAhkiAAAAo5JREFUSInV1k+IlVUYx/GPkxubFq1CwlRQtNy4SLBCXZh/QtCJoIWCChYSKmSJf7HhLhSFIoqSQSMw2lUQ/mPQdFM6CQ5KA1aLxM2AYWG2sb+MLp7z0st73/feq+8wMT+4nPc+59/3nOc5zzkTGo2G8aCumv2fw118WVI3Eb/hdvqupbqgF3ETyzCpULcIj6If/9acpzboCE7hYSwp1PWk8njNOVAflP9Aegr2VWIn+0dhjibQMyLmXirYJ+BoqjtY0udPrMyNNxfT8Y2I0by2pXG2VTDNxl/4uhXoduHOfXgoZ38H6/ERdhX63ME5PIZnk62V28+n8pkK0A/S3FtagX6HT/EU1ibbHryJz/BaxeBF969K5YmStpfxB+aX1L2MpTiEoVagsFe4siFWtR+nE/hIBegJ4c4eTMHT+B7XStr+g0t4Ao/n7N14V2SR3mKnMtBhvIdpwg0DImb/roCEGxjELOxMtlan/UIq8+7vFYvcid87AYVfct+viDhspwxsUyrL3J4pA83c/yTewLf4pKxDGehqcXh+Tv9f7wAyD9olFnqxRdsBESrZjn4oDtDmZG9S8WpbIVZ0FYtFingV7+PHNqBDIsa6cUV1PBNX6w+YhzV4Hn2pX6nyoAvwhYjRZWJX3sLnIne+2AaU6txYpvOYg8P4VRziSmWgc3FSBPFScTgk8EFxmheKBJ5Xu13ONIANBdsFbMQjIj5vtQOdKdLPXSzXnFJ24yu8rTlJz+4QdLjEdj2Vl/BxuwEm4idMbtHmrLhCy1Rl70TZLVh5gPIajUfJg2iNeBv0iR1tq9oP2vvQVAE4A+tEZtnRaeexBH0BB8Rr6hi26uwiwdiCHkm/B9L/FaP3rXEDeg8zcooVsnPo6gAAAABJRU5ErkJggg==\n",
      "text/latex": [
       "$\\displaystyle x \\veebar y$"
      ],
      "text/plain": [
       "x ⊻ y"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Xor(x, y)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于 Python 和 SymPy 之间的运算, 结果最终都会是 SymPy 的数据类型. 对于出发运算, SymPy 的结果是一个比例, 这不同于Python中的结果. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Words\n",
    "\n",
    "| 英文 | 词性 | 中文 | \n",
    "| - | - | - | \n",
    "| infallible | adj. | 绝对可靠的 | "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
